#pragma once

#include <string>
#include <fstream>
#include <atomic>
#include <vector>
#include <iomanip>
#include <random>
#include <cstring>
#include <sstream>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        // 获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };

    const std::string temp_path = "./temp/";
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;
            return path_name;
        }

        // 编译时需要产生的临时文件
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        // 运行时需要产生的临时文件
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            {
                // 获取属性成功,文件已经存在
                return true;
            }
            return false;
        }

        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target);
            if (!out.is_open())
            {
                return false;
            }

            out.write(content.c_str(), content.size());

            out.close();
            return true;
        }

        static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
        {
            std::ifstream in(target);
            if (!in.is_open())
            {
                return false;
            }

            std::string line;
            // getline会去掉\n
            while (std::getline(in, line))
            {
                *content += line;
                *content += (keep ? "\n" : "");
            }

            in.close();
            return true;
        }

        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
            std::string ms = TimeUtil::GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;
        }
    };

    class StringUtil
    {
    public:
        static void Split(const std::string &src, const std::string &sep, std::vector<std::string> *out)
        {
            // 使用boost库提供的字符串分割
            //  boost::algorithm::token_compress_on 去除多余的空格字符
            boost::split((*out), src, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };

    class UUIDHelper
    {
    public:
        static std::string uuid()
        {
            std::random_device rd;
            std::mt19937 generator(rd());
            std::uniform_int_distribution<int> distribution(0, 255);

            std::stringstream ss;
            // 将8个（0-255）数字变成16进制数字字符
            for (int i = 0; i < 8; i++)
            {
                ss << std::setw(2) << std::setfill('0') << std::hex << distribution(generator);
                if (i == 3 || i == 5 || i == 7)
                {
                    ss << '-';
                }
            }

            // 8字节数字序号
            static std::atomic<size_t> number(1);
            size_t num = number.fetch_add(1);

            for (int i = 7; i >= 0; i--)
            {
                //((num >> ((i * 8))) & 0xFF)每次取8位bit
                ss << std::setw(2) << std::setfill('0') << std::hex << ((num >> ((i * 8))) & 0xFF);
                if (i == 6)
                {
                    ss << '-';
                }
            }

            return ss.str();
        }
    };
}
